<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="项目" label-width="70px">
          <el-select
            v-model="searchParam.projectId"
            placeholder="请选择项目"
            clearable
          >
            <el-option
              v-for="item in projectList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工序" label-width="70px">
          <el-select
            v-model="searchParam.processId"
            placeholder="请选择工序"
            clearable
          >
            <el-option
              v-for="item in processList"
              :key="item.id"
              :label="item.processName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作类型" label-width="70px">
          <el-select
            v-model="searchParam.opeTypeCode"
            placeholder="请选择操作类型"
            clearable
            @change="changeOpeType"
          >
            <el-option
              v-for="item in useDictionary('CheckOperateType')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="事务类型" label-width="70px">
          <el-select
            v-model="searchParam.effectTypeCode"
            placeholder="请选择事务类型"
            clearable
          >
            <el-option
              v-for="item in effectTypeCodeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检验单号" label-width="70px">
          <el-input
            v-model="searchParam.effectCheckCode"
            placeholder="请输入检验单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="预检单号" label-width="70px">
          <el-input
            v-model="searchParam.effectPreCheckCode"
            placeholder="请输入预检单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!-- <template #header> </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="检验单号"
          prop="effectCheckCode"
          min-width="150"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.effectCheckCode ?? scope.row.orderNo }}
          </template>
        </el-table-column>
        <el-table-column
          label="事务类型"
          prop="effectTypeCode"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ getEffectTypeCode(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          label="检验项目名称"
          prop="checkItemName"
          min-width="140"
          show-overflow-tooltip
          align="center"
        />
        <!-- <el-table-column
          label="项目"
          prop="projectName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        /> -->
        <el-table-column
          label="工序"
          prop="processName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="预检单号"
          prop="effectPreCheckCode"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="任务单号"
          prop="taskId"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="检验类型"
          prop="checkTypeName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="检验结果"
          prop="checkResult"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.checkResult === 1" type="success"
              >合格</el-tag
            >
            <el-tag v-if="scope.row.checkResult === 0" type="danger"
              >不合格</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="生产时间"
          prop="productTime"
          min-width="150"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="班次"
          prop="classes"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="操作类型"
          prop="opeTypeCode"
          min-width="120"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("CheckOperateType", scope.row.opeTypeCode) }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="160" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看明细
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail1(scope.row)"
              ><i-ep-edit />
              录入
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qmscheckdoc-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :ruleData="addDialog.ruleData"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <qmscheckpre-mock
      :visible="addDialog1.visible"
      :title="addDialog1.title"
      :data="addDialog1.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog1.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRoute } from "vue-router";
import { checkCommon } from "@/views/qms/checkHook/checkCommon";

const route = useRoute();
defineOptions({
  name: "QMSQMSCHECKDOC",
  inheritAttrs: false,
});

const props = defineProps({
  sopType: {
    type: String,
    default: "",
  },
});

// 请求参数
const requestApi = "/qms/qmscheckdoc/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  effectCheckCode: "",
  sopTypeCode: props.sopType,
};

const deleteParam = {
  api: "/qms/qmspointcheckorder/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const { effectTypeCodeList, getEffectTypeCode, changeOpeType, resetHandle } =
  checkCommon(searchParam, reset);
const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  ruleData: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/qms/qmspointcheckorder/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

const getRule = async (checkRuleId: string) => {
  const { data } = await request({
    url: `/qms/qmscheckrule/get/${checkRuleId}`,
    method: "get",
  });
  addDialog.ruleData = data;
};
// 查询详情
async function getStandard(id: string) {
  request({
    // url: `/qms/qmspointcheckorder/getStandard/${id}`,
    url: `/qms/qmscheckdoc/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
/** 打开标准检验检验单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "标准检验检验单详情";
  if (roleObj.checkRuleId !== null && roleObj.checkRuleId !== "") {
    getRule(roleObj.checkRuleId);
  }
  getStandard(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  addDialog1.visible = value;
  if (!value) {
    addDialog.data = {};
    addDialog1.data = {};
  }
}

const projectList = ref([]);

const getProjectList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprojectinfo/getList",
    method: "post",
    data: {},
  });
  projectList.value = data;
};

const processList = ref([]);

const getProcessList = async () => {
  const { data } = await request({
    url: "/mdm/mdmprocess/getList",
    method: "post",
    data: {},
  });
  processList.value = data;
};

onMounted(() => {
  getProjectList();
  getProcessList();
  searchParam.value.effectPreCheckCode = route.query?.effectPreCheckCode;
  searchParam.value.sopTypeCode = props.sopType;
  getTableList();
});

const addDialog1 = reactive<DialogOption>({
  visible: false,
  title: "",
  readvisible: false,
  data: {},
});
/** 打开标准检验预检单表单弹窗 */
function openDialog1(roleObj?: Object) {
  if (roleObj) {
    addDialog1.title = "录入检验单";
    addDialog1.readvisible = false;
    getInfo1(roleObj.id);
  } else {
    // addDialog1.title = "新增标准预检单";
    // addDialog1.visible = true;
    // addDialog1.readvisible = false;
  }
}

/** 打开标准检验预检单表单弹窗 详情 */
function openDialogdetail1(roleObj?: any) {
  addDialog1.title = "录入检验单";
  addDialog1.readvisible = true;
  getInfo1(roleObj.id);
}

// 查询详情
async function getInfo1(id: string) {
  request({
    url: `/qms/qmscheckdoc/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog1.data = data.data;
      addDialog1.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}
</script>
